﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MaintenanceUpdates.CUiMaintenanceUpdateDownloadWindow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using Microsoft.Web.WebView2.Core;
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.MaintenanceUpdates;

public class CUiMaintenanceUpdateDownloadWindow : TxWindow, IComponentConnector
{
  private Cursor m_currentCursor;
  public bool DownloadedSuccessfully;
  public string DownloadedFilePath;
  private bool m_deleteCookies;
  internal TxWebView2 _browser;
  private bool _contentLoaded;

  public CUiMaintenanceUpdateDownloadWindow(string url, bool deleteCookies)
  {
    this.InitializeComponent();
    ((Microsoft.Web.WebView2.Wpf.WebView2) this._browser).Source = new Uri(url);
    this.m_deleteCookies = deleteCookies;
  }

  public void SetSource(string url) => ((Microsoft.Web.WebView2.Wpf.WebView2) this._browser).Source = new Uri(url);

  private void _browser_CoreWebView2InitializationCompleted(
    object sender,
    CoreWebView2InitializationCompletedEventArgs e)
  {
    Color activeTextColor = this._browser.ActiveTextColor;
    if (this.m_deleteCookies)
      ((Microsoft.Web.WebView2.Wpf.WebView2) this._browser).CoreWebView2.CookieManager.DeleteAllCookies();
    string connectingToSsc = CApMaintenanceUpdatesResources.CONNECTING_TO_SSC;
    ((Microsoft.Web.WebView2.Wpf.WebView2) this._browser).ExecuteScriptAsync($"document.querySelector('body').innerHTML = '<p style=\"font -family:Arial;font-size:16px;color:rgb({activeTextColor.R},{activeTextColor.G}, {activeTextColor.B});\">{connectingToSsc}</p>';");
    ((Microsoft.Web.WebView2.Wpf.WebView2) this._browser).CoreWebView2.DownloadStarting += new EventHandler<CoreWebView2DownloadStartingEventArgs>(this.CoreWebView2_DownloadStarting);
  }

  private void CoreWebView2_DownloadStarting(object sender, CoreWebView2DownloadStartingEventArgs e)
  {
    this.m_currentCursor = ((FrameworkElement) this).Cursor;
    CoreWebView2Deferral deferral = e.GetDeferral();
    SynchronizationContext.Current.Post((SendOrPostCallback) (_ =>
    {
      using (deferral)
      {
        e.Handled = true;
        e.DownloadOperation.StateChanged += new EventHandler<object>(this.DownloadOperation_StateChanged);
      }
    }), (object) null);
  }

  private void DownloadOperation_StateChanged(object sender, object e)
  {
    if (!(sender is CoreWebView2DownloadOperation downloadOperation))
      return;
    CoreWebView2DownloadState state = downloadOperation.State;
    if (state != 1 && state == 2)
    {
      this.DownloadedFilePath = downloadOperation.ResultFilePath;
      this.DownloadedSuccessfully = true;
    }
    downloadOperation.StateChanged -= new EventHandler<object>(this.DownloadOperation_StateChanged);
    ((FrameworkElement) this).Cursor = this.m_currentCursor;
    ((Window) this).Close();
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/maintenanceupdates/cuimaintenanceupdatedownloadwindow.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    if (connectionId == 1)
    {
      this._browser = (TxWebView2) target;
      ((Microsoft.Web.WebView2.Wpf.WebView2) this._browser).CoreWebView2InitializationCompleted += new EventHandler<CoreWebView2InitializationCompletedEventArgs>(this._browser_CoreWebView2InitializationCompleted);
    }
    else
      this._contentLoaded = true;
  }
}
